package com.baoyi.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class UpdateCityService extends Service {
	public UpdateCityService() {
	}

	@Override
	public IBinder onBind(Intent intent) {
		throw new UnsupportedOperationException("Not yet implemented");
	}
	public LocationClient mLocationClient = null;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mLocationClient = new LocationClient(this);
		LocationClientOption option=new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("all");
		option.setPoiExtraInfo(true);
		mLocationClient.setLocOption(option);


	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		Log.i("ada", "查询你的位置");
		mLocationClient.registerLocationListener(new BDLocationListener() {

					@Override
					public void onReceivePoi(BDLocation arg0) {
						if (arg0 == null) {
							return;
						}
						String city = arg0.getCity();
						if(city!=null){
							saveCity(city);
							mLocationClient.stop();
						}
					
					}


					@Override
					public void onReceiveLocation(BDLocation arg0) {
						if (arg0 == null) {
							return;
						}
						String city = arg0.getCity();
						if(city!=null){
							saveCity(city);
							mLocationClient.stop();
						}

					}
				});
		mLocationClient.start();
		return super.onStartCommand(intent, flags, startId);
	}

	public void saveCity(String name) {
		PreferenceUtils.setStringValue(this, "cityname", name);
	}
}
